package com.hansming.bss.modules.sfc.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hansming.bss.common.service.CrudService;
import com.hansming.bss.modules.sfc.dao.CarDatePriceDao;
import com.hansming.bss.modules.sfc.dao.CarInfoDao;
import com.hansming.bss.modules.sfc.dao.CarServeDao;
import com.hansming.bss.modules.sfc.entity.CarDatePrice;
import com.hansming.bss.modules.sfc.entity.CarInfo;
import com.hansming.bss.modules.sfc.entity.CarServe;
import com.hansming.bss.modules.sfc.util.TimeUtil;

/**
 * 房车日期价格信息Service
 * @author Hansming
 * @version 2017-09-18
 */
@Service
@Transactional(readOnly = true)
public class CarDatePriceService extends CrudService<CarDatePriceDao,CarDatePrice>{
	
	@Autowired
	private CarDatePriceDao carDatePriceDao;
	
	@Autowired
	private CarInfoDao carinfoDao;
	
	@Autowired
	private CarServeDao carserveDao;
	
	//根据房车id
	public List<CarDatePrice> getByCarId(String id){
		List<CarDatePrice> list = carDatePriceDao.getByCarId(id);
		return list;
	}
	
	public List<CarDatePrice> getByDateFrom(String fromDate,String toDate,CarInfo carinfo){
		return carDatePriceDao.getByDateFrom(fromDate,toDate,carinfo);
	}
	
		//得到某一日的价格
		public CarDatePrice getByDate(String carid,String datetime){
			return carDatePriceDao.getByDate(carid,datetime);
		}
	
		//得的该型号租车的定价的最早时间
		public	Date getEarlyTimeByCarid(String carid){
			return carDatePriceDao.getEarlyTimeByCarid(carid);
		}
		
		//得到该型号租车的定价的最晚时间
		public	Date getLastTimeByCarid(String carid){
			return carDatePriceDao.getLastTimeByCarid(carid);
		}
		
		//根据日期修改价格
		@Transactional(readOnly = false)
		public void updateByDate(CarDatePrice cardateprice){
			carDatePriceDao.updateByDate(cardateprice);
		}
		
		//根据日期修改价格不通过对象
		@Transactional(readOnly = false)
		public void updatebydatenoobject(String carid,String datetime,BigDecimal price){
			carDatePriceDao.updatebydatenoobject(carid, datetime, price);
		}
		
		//根据日期修改价格不通过对象
		@Transactional(readOnly = false)
		public void savedatepricenoobject(String carid,String datetime,BigDecimal price,String id){
			carDatePriceDao.savedatepricenoobject(carid, datetime, price, id);
		}
		
		public List<CarDatePrice> getthreeprice(String carid,String first,String second,String third){
			return carDatePriceDao.getthreeprice(carid, first, second, third);
		};
		
		public List<CarDatePrice> getcardateprice(String carid,String sdate,String edate){
			return carDatePriceDao.getcardateprice(carid, sdate, edate);
		}
		
		
		/*
		 * 得到条件选择下的价格
		 * @Param
		 * 		id  车辆标识id
		 * 		startDate 起始日期 yyyy-MM-dd
		 * 		endDate  还车日期 yyyy-MM-dd
		 * 		starttime  整型数字  0代表00：00，1代表0：30，2代表01：00以此类推，最大47代表23：00
		 * 		serveid  为“|”分隔的服务id字符串；
		 * @Result
		 * 		BigDecimal 总价格（计算得到的总价格）
		 */
		public BigDecimal getTotlePrice(String startDate,String endDate,int starttime,int endtime,String id,String serveid) throws ParseException{
			BigDecimal totleprice = new BigDecimal("0.00");		//准备计算总价格的基础
			SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
			CarInfo carinfo = carinfoDao.get(id);
			Date fdate = dft.parse(startDate);
			Calendar todatecal = Calendar.getInstance();
			todatecal.setTime(dft.parse(endDate));
			todatecal.set(Calendar.DATE, todatecal.get(Calendar.DATE)-1);
			Date tdate = dft.parse(dft.format(todatecal.getTime()));
			//若超过中午12点，则按照翌日的价格计算，起始的结束日期加1以下只为日期加1
			if(starttime >24){											
				Calendar fromcal = Calendar.getInstance();
				fromcal.setTime(dft.parse(startDate));
				fromcal.set(Calendar.DATE, fromcal.get(Calendar.DATE) + 1);
				fdate = dft.parse(dft.format(fromcal.getTime()));
				Calendar tocal = Calendar.getInstance();
				tocal.setTime(dft.parse(endDate));
				tocal.set(Calendar.DATE, tocal.get(Calendar.DATE));
				tdate = dft.parse(dft.format(tocal.getTime()));
				}	
			int hour = 0;										//多出整天数的时间，超过四小时算作一天，若不超过，按照比例每半小时为该天价格的1/16，单位为半小时
			int resulthour =endtime - starttime; 					//根据取车的时间 starttime 为 取车时间的id,id为0 = 00:00,id为1 = 00:30以此为例（tohour 同理)
			if(endtime == 0){
				Calendar tocal = Calendar.getInstance();
				tocal.setTime(tdate);
				tocal.set(Calendar.DATE, tocal.get(Calendar.DATE)-1);
				tdate = dft.parse(dft.format(tocal.getTime()));
			}
			BigDecimal extrPrice = new BigDecimal("0.00");				//超出整天数的小时所产生的额外价格
			if(resulthour >= 0 ){
				//超过一天的时间
				if(resulthour >8){
					Calendar tocal = Calendar.getInstance();			//days = days +1;
					tocal.setTime(tdate);
					tocal.set(Calendar.DATE, tocal.get(Calendar.DATE) + 1);
					tdate = dft.parse(dft.format(tocal.getTime()));
				}else{
					hour = resulthour;			//此为小时数为超过整天数4小时内，按每半小时当天单价的1/16计价
					//超出时间的价格计算，得出当日的价格/16*超出的时间
					CarDatePrice cardateprice = new CarDatePrice();
					cardateprice.setCarInfo(carinfo);
					cardateprice.setDatetime(dft.format(tdate));
					extrPrice = carDatePriceDao.getByDate(cardateprice.getCarInfo().getId(),cardateprice.getDatetime()).getPrice().divide(new BigDecimal("16")).multiply(new BigDecimal(String.valueOf(hour)));
					extrPrice = extrPrice.setScale(2,BigDecimal.ROUND_UP);
				}
			}else{
				if(resulthour < -40){
					hour = 48 + resulthour;
					CarDatePrice cardateprice = new CarDatePrice();
					cardateprice.setCarInfo(carinfo);
					cardateprice.setDatetime(dft.format(tdate));
					extrPrice = carDatePriceDao.getByDate(cardateprice.getCarInfo().getId(),cardateprice.getDatetime()).getPrice().divide(new BigDecimal("16")).multiply(new BigDecimal(String.valueOf(hour)));
					extrPrice = extrPrice.setScale(2,BigDecimal.ROUND_UP);
					Calendar tocal = Calendar.getInstance();
					tocal.setTime(tdate);
					tocal.set(Calendar.DATE, tocal.get(Calendar.DATE)-1);
					tdate = dft.parse(dft.format(tocal.getTime()));
				}
			}
			//得到该计算日期的时间段的价格总价
			List<CarDatePrice>	carpirces = carDatePriceDao.getByDateFrom(dft.format(fdate),dft.format(tdate),carinfo);
			if(carpirces != null){
				for(CarDatePrice p:carpirces){
					totleprice = totleprice.add(p.getPrice());
				}
			}
			
			//加上额外的价格			
			totleprice = totleprice.add(extrPrice);

			int day = TimeUtil.Datecalc(startDate,endDate);
			String[] serves = serveid.split("\\|");
			BigDecimal price = new BigDecimal("0.00");//计算服务总价
			System.out.println(serves);
			//首先计算总价的订单取消险价格，总价会影响订单取消险的价格
			for(int i=0;i<serves.length;i++){
				if("7".equals(serves[i])){		//订单取消服务
					price = totleprice.multiply(new BigDecimal("0.04"));
					price = price.setScale(2,BigDecimal.ROUND_UP);
					totleprice = totleprice.add(price);
				}
			}
			//之后其他服务可随意增加价格
			for(int i=0;i<serves.length;i++){
				if(serves[i] != null && serves[i] != ""){
					if("5".equals(serves[i])){				//司机代驾服务
						CarServe serve = carserveDao.get("5");				
						price = serve.getCaritemprice().multiply(new BigDecimal(String.valueOf(day)));
						price = price.setScale(2,BigDecimal.ROUND_UP);
						totleprice = totleprice.add(price);
					}else if("6".equals(serves[i])){		//不计免赔服务
						CarServe serve = carserveDao.get("6");				
						price = serve.getCaritemprice().multiply(new BigDecimal(String.valueOf(day+1)));
						price = price.setScale(2,BigDecimal.ROUND_UP);
						totleprice = totleprice.add(price);
					}else if("8".equals(serves[i])){
						CarServe serve = carserveDao.get("8");				//承租人责任险
						price = serve.getCaritemprice().multiply(new BigDecimal(String.valueOf(day+1)));
						price = price.setScale(2,BigDecimal.ROUND_UP);
						System.out.println(totleprice);
						totleprice = totleprice.add(price);
						System.out.println(price+"///////"+totleprice);
					}
				}	
			}
			
			
			return totleprice;
		}
		
}
